import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"
import axios from "axios"

interface ProductSearchState {
  loading: boolean,
  error: string | null,
  data: any,
  pagination: any
}

const initialState: ProductSearchState = {
  loading: true,
  error: null,
  data: null,
  pagination: null
}

export const searchProduct = createAsyncThunk(
  'productSearch/searchProduct',
  async (paramaters: { keywords: string, nextPage: number | string, pageSize: number | string }, thunkAPI) => {
    let url = `http://82.157.43.234:8080/api/touristRoutes?pageNumber=${paramaters.nextPage}&pageSize=${paramaters.pageSize}`
    if (paramaters.keywords) {
      url += `&keyword=${paramaters.keywords}`
    }
    const response = await axios.get(url, {
      headers: {
        'x-icode': 'EDE45279EB142953'
      }
    })
    return {
      data: response.data,
      pagination: JSON.parse(response.headers['x-pagination']) 
    }

  }
)


export const productSearchSlice = createSlice({
  name: 'productSearch',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(searchProduct.pending, (state) => {
        state.loading = true
      })
      .addCase(searchProduct.fulfilled, (state, action) => {
        state.data = action.payload.data
        state.pagination = action.payload.pagination
        state.loading = false
        state.error = null
      })
      .addCase(searchProduct.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }

      })
  }
})